package com.willcode.demo.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * @author Will
 */
@Slf4j
@Aspect
public class LogAspects {

    @Pointcut("execution(public String com.willcode.demo.aop.Demo4Aop.*(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void logStart() {
        log.info("日志记录开始-logStart");
    }

    @After("pointCut()")
    public void logEnd() {
        log.info("日志记录结束-logEnd");
    }

    @AfterReturning("pointCut()")
    public void logReturn() {
        log.info("日志记录返回值-logReturn");
    }

    @AfterThrowing("pointCut()")
    public void logExpection() {
        log.info("日志记录异常-logException");
    }

    @Around("pointCut()")
    public Object arround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        StringBuilder sb = new StringBuilder();
        Object[] args = proceedingJoinPoint.getArgs();
        for (Object arg : args) {
            sb.append(arg).append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        log.info("@Around：执行目标方法之前，其中参数-" + sb.toString());
        Object obj = proceedingJoinPoint.proceed();
        log.info("@Around：执行目标方法之后，其中返回值-" + obj);
        return obj;
    }
}